package cn.burnit.view;

import cn.burnit.pojo.Book;
import cn.burnit.pojo.Borrow;
import cn.burnit.pojo.Reader;
import cn.burnit.pojo.ReaderBorrowBook;
import cn.burnit.service.BookService;
import cn.burnit.service.BorrowService;
import cn.burnit.service.ReaderService;
import org.jdesktop.swingx.JXDatePicker;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

/**
 * 还书
 */
public class ReturnBookForm extends JFrame {
    private JLabel bookId,readerId,bookName,publisher,price,author,publishTime,stock
            ,readerName,readerType,max_num,days_num
            ,returnDate
            ,readerInfo,borrowInfo,bookInfo;
    private JTextField bookIdField,readerIdField,bookNameField,authorField,publisherField,priceField,stockField
            ,readerNameField,readerTypeField,max_numField,days_numField
            ;
    private JButton btn_Check,btn_Back,btn_Close;
    private BorrowService borrowService=new BorrowService();
    private BookService bookService=new BookService();
    final JXDatePicker datepick1,datepick2;


    public ReturnBookForm() {
        Container container = getContentPane();
        container.setLayout(null);

        btn_Check=new JButton("查询");
        btn_Check.setBounds(450,20,60,20);
        btn_Back=new JButton("还书");
        btn_Back.setBounds(200,380,60,20);
        btn_Close=new JButton("关闭");
        btn_Close.setBounds(300,380,60,20);
        btn_Close.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
        btn_Check.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                checkReaderIdAndBookId(e);
            }
        });
        btn_Back.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                returnBook(e);
            }
        });

        bookId=new JLabel("图书编号");
        bookId.setFont(new Font("宋体", Font.PLAIN, 16));
        bookId.setBounds(50,20,100,20);
        readerId=new JLabel("读者编号");
        readerId.setFont(new Font("宋体", Font.PLAIN, 16));
        readerId.setBounds(250,20,100,20);
        bookInfo=new JLabel("--------------------------图书信息--------------------------");
        bookInfo.setFont(new Font("宋体", Font.PLAIN, 16));
        bookInfo.setBounds(50,55,500,20);
        bookName=new JLabel("图书名称:");
        bookName.setFont(new Font("宋体", Font.PLAIN, 16));
        bookName.setBounds(50,80,100,20);
        author=new JLabel("作者:");
        author.setFont(new Font("宋体", Font.PLAIN, 16));
        author.setBounds(350,80,100,20);
        publisher=new JLabel("出版社:");
        publisher.setFont(new Font("宋体", Font.PLAIN, 16));
        publisher.setBounds(50,110,100,20);
        publishTime=new JLabel("出版时间:");
        publishTime.setFont(new Font("宋体", Font.PLAIN, 16));;
        publishTime.setBounds(350,110,100,20);
        price=new JLabel("订价:");
        price.setFont(new Font("宋体", Font.PLAIN, 16));
        price.setBounds(50,140,100,20);
        stock=new JLabel("库存量:");
        stock.setFont(new Font("宋体", Font.PLAIN, 16));
        stock.setBounds(350,140,100,20);
        readerInfo=new JLabel("--------------------------读者信息--------------------------");
        readerInfo.setFont(new Font("宋体", Font.PLAIN, 16));
        readerInfo.setBounds(50,170,500,20);
        readerName=new JLabel("读者姓名:");
        readerName.setFont(new Font("宋体", Font.PLAIN, 16));
        readerName.setBounds(50,195,100,20);
        readerType=new JLabel("读者类型:");
        readerType.setFont(new Font("宋体", Font.PLAIN, 16));
        readerType.setBounds(350,195,100,20);
        max_num=new JLabel("最大可借数:");
        max_num.setFont(new Font("宋体", Font.PLAIN, 16));
        max_num.setBounds(50,220,100,20);
        days_num=new JLabel("最大可借天数:");
        days_num.setFont(new Font("宋体", Font.PLAIN, 16));
        days_num.setBounds(350,220,130,20);
        borrowInfo=new JLabel("--------------------------还书信息--------------------------");
        borrowInfo.setFont(new Font("宋体", Font.PLAIN, 16));
        borrowInfo.setBounds(50,250,500,20);

        bookIdField=new JTextField();
        bookIdField.setBounds(120,20,100,20);
        readerIdField=new JTextField();
        readerIdField.setBounds(320,20,100,20);
        bookNameField=new JTextField();
        bookNameField.setEditable(false);
        bookNameField.setBounds(140,80,100,20);
        authorField=new JTextField();
        authorField.setEditable(false);
        authorField.setBounds(430,80,100,20);
        publisherField=new JTextField();
        publisherField.setEditable(false);
        publisherField.setBounds(140,110,100,20);
        //出版时间
        Date date = new Date();
        // 设置 date日期
        datepick1= new JXDatePicker();
        datepick1.setDate(date);
        datepick1.setEditable(false);
        datepick1.setBounds(430,110,100,20);

        priceField=new JTextField();
        priceField.setEditable(false);
        priceField.setBounds(140,140,110,20);
        stockField=new JTextField();
        stockField.setEditable(false);
        stockField.setBounds(430,140,100,20);
        readerNameField=new JTextField();
        readerNameField.setEditable(false);
        readerNameField.setBounds(140,195,100,20);
        readerTypeField=new JTextField();
        readerTypeField.setEditable(false);
        readerTypeField.setBounds(430,195,100,20);
        max_numField=new JTextField();
        max_numField.setEditable(false);
        max_numField.setBounds(140,220,100,20);
        days_numField=new JTextField();
        days_numField.setEditable(false);
        days_numField.setBounds(470,220,60,20);
        returnDate=new JLabel("还书日期:");
        returnDate.setFont(new Font("宋体", Font.PLAIN, 16));
        returnDate.setBounds(50,275,100,20);
        //借阅日期
        Date date2 = new Date();
        // 设置 date日期
        datepick2= new JXDatePicker();
        datepick2.setDate(date2);
        datepick2.setBounds(140,275,100,20);


        container.add(bookId);container.add(bookIdField);
        container.add(readerId);container.add(readerIdField);
        container.add(btn_Check);
        container.add(bookInfo);
        container.add(bookName);container.add(bookNameField);
        container.add(author);container.add(authorField);
        container.add(publisher);container.add(publisherField);
        container.add(publishTime);container.add(datepick1);
        container.add(price);container.add(priceField);
        container.add(stock);container.add(stockField);
        container.add(readerInfo);
        container.add(readerName);container.add(readerNameField);
        container.add(readerType);container.add(readerTypeField);
        container.add(max_num);container.add(max_numField);
        container.add(days_num);container.add(days_numField);
        container.add(borrowInfo);
        container.add(returnDate);container.add(datepick2);
        container.add(btn_Back);container.add(btn_Close);

        setTitle("归还图书");
        setSize(600,500);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void returnBook(ActionEvent e) {
        //获取图书id
        String bookId = bookIdField.getText();
        //获取库存量
        int stock = Integer.parseInt(this.stockField.getText());
        //获取最大可借数
        int maxnum= Integer.parseInt(max_numField.getText());
        //获取读者id
        int readerId = Integer.parseInt(readerIdField.getText());
        //获取当前日期
        Date date = this.datepick2.getDate();
        java.sql.Date latelyDate = new java.sql.Date(date.getTime());
        //设置借书和还书的标识
        String if_back="";
        Borrow borrow=borrowService.findBorrowById(readerId);
        if (borrow!=null){
            if_back="0";
            Borrow bookIfbackReturn = new Borrow(bookId,readerId,latelyDate,if_back);
            //修改
            int i=borrowService.updateBackDate(bookIfbackReturn);
            if (i>0){
                //最大可借数+1；
                maxnum++;
                //库存+1
                stock++;
                BookService bookService = new BookService();
                Book book = new Book(bookId,stock);
                Reader reader = new Reader(readerId,maxnum);
                ReaderService readerService = new ReaderService();
                //更新读者可借的最大数量
                int i2 = readerService.updateReaderMaxnum(reader);
                //更新库存量
                int i1 = bookService.updateBookStock(book);
                if (i1>0&&i2>0){
                    //更新后设置库存文本框
                    stockField.setText(String.valueOf(book.getStock()));
                    max_numField.setText(String.valueOf(reader.getMax_num()));
                    JOptionPane.showMessageDialog(null,"还书成功");
                    dispose();
                }

            }else {
                JOptionPane.showMessageDialog(null,"还书失败");
            }
        }
    }

    private void checkReaderIdAndBookId(ActionEvent e) {
        String bookId = bookIdField.getText();//获取图书id
        int readerId = Integer.parseInt(readerIdField.getText());//获取读者id
        //System.out.println(readerId);
        ReaderBorrowBook rbb=borrowService.findBorrowBybookIdAndreaderId(bookId,readerId);
        //System.out.println(rbb);
        if (rbb!=null){
            bookNameField.setText(rbb.getBookname());//书名
            authorField.setText(rbb.getAuthor());//作者
            publisherField.setText(rbb.getPublisher());//出版社
            datepick1.setDate(rbb.getPublish_time());//出版日期
            priceField.setText(String.valueOf(rbb.getPricce()));//价格
            stockField.setText(String.valueOf(rbb.getStock()));//库存
            readerNameField.setText(rbb.getReadername());//读者名
            readerTypeField.setText(rbb.getReadertype());//读者类型
            max_numField.setText(String.valueOf(rbb.getMax_num()));//最大借书量
            days_numField.setText(String.valueOf(rbb.getDays_num()));//最大借书天数
            Borrow borrow = borrowService.findBorrowById(readerId);//通过读者id查看borrow
            //System.out.println(borrow);
            //设置还书时间
            if(borrow.getLatelyDate()!=null){
                datepick2.setDate(borrow.getLatelyDate());
                btn_Back.setEnabled(false);
            }
        }
    }

    public static void main(String[] args) {
        new ReturnBookForm();
    }
}
